<template>
    <van-nav-bar
    title="收货地址"
    left-text="返回"
    left-arrow
    @click-left="onClickLeft"
    />
    <van-address-list
    v-model="chosenAddressId"
    :list="list"
    :disabled-list="disabledList"
    disabled-text="以下地址超出配送范围"
    default-tag-text="默认"
    @add="onAdd"
    @edit="onEdit"
    @select="onSelect"
    />
</template>

<script setup>
import { onMounted, reactive } from "vue";

    const {proxy} = getCurrentInstance()
    let userInfo = proxy.$cookies.get("business");

    // 地址列表
    let list = reactive([]);
    //选中项，值对应list中的id
    let chosenAddressId = ref('1');
    // 超出地址列表
    const disabledList = reactive([])

    // 挂载前获取地址数据
    onBeforeMount(()=>{
        getAddressData()
    })

    const getAddressData = async ()=>{
        // 发送请求
        let data = {
            busid:userInfo.id
        }
        let result = await proxy.$POST({
            url:"/address/index",
            params:data
        })
        if(result.code && result.data){
            result.data.forEach(item => {
                if(item.status == 1){
                    chosenAddressId.value = item.id
                }
                list.push({
                    id: item.id,
                    name: item.consignee,
                    tel: item.mobile,
                    address: item.region_text,
                    isDefault: item.status==1?true:false,
                    areaCode:item.district?item.district:(item.city?item.city:(item.province?item.province:"")),
                    addressDetail:item.address,
                })
            });
        }else{
            proxy.$fail(result.msg)
            return false
        }
    }
   
    // 返回
    const onClickLeft = () => history.back();

    // 新增方法
    const onAdd = () => {
        // 跳转到新增界面
        proxy.$router.push("/address/add")
    };

    // 编辑方法
    const onEdit = (item, index) => {
        // return false
        proxy.$router.push({
            name:"addrssAdd",
            query:item
        })
    };

    // 截流标记
    let flag = false
    // 判断是否是选择订单地址
    let action = proxy.$route.query.action?proxy.$route.query.action:""
    // 选择方法
    const onSelect = async (item,index) => {
        if(flag){
            proxy.$fail("操作太快了,出错了");
            return false
        }
        flag = true
        if(action){
            proxy.$cookies.set("address",item)
            flag = false
        }else{
            // 初始化
            list.forEach(item=>{
                item.isDefault = false
            })
            item.isDefault = true
            let data = {
                busid:userInfo.id,
                id:item.id
            }
            // 发送请求
            let result = await proxy.$POST({
                url:"/address/select",
                params:data
            })
            if(!result.code){
                proxy.$fail({
                    message:"切换默认地址失败",
                    duration:1000,
                    onClose:()=>{
                        list = reactive([])
                        getAddressData()
                    }
                })
                return false
            }else{
                flag = false
            }
        }
        
    }
</script>